package com.apkcore.camerax

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.Camera
import androidx.camera.core.CameraFilter
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.core.UseCaseGroup
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import com.google.common.util.concurrent.ListenableFuture
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

class MainActivity : AppCompatActivity() {

    private lateinit var frontTextureView: PreviewView
//    private lateinit var backTextureView: PreviewView
    private lateinit var cameraExecutor: ExecutorService
    private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
    private lateinit var camera: Camera
//    private lateinit var camera2: Camera


    // 为每个摄像头分配独立的生命周期所有者
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        frontTextureView = findViewById(R.id.front_textureView)
//        backTextureView = findViewById(R.id.back_textureView)

        cameraExecutor = Executors.newSingleThreadExecutor()
        cameraProviderFuture = ProcessCameraProvider.getInstance(this)

        cameraProviderFuture.addListener({
            val cameraProvider = cameraProviderFuture.get()

            val frontPreview = Preview.Builder()
                .setTargetRotation(frontTextureView.display.rotation)
                .build()
                .also {
                    it.setSurfaceProvider(frontTextureView.surfaceProvider)
                }

//            val backPreview = Preview.Builder()
//                .setTargetRotation(backTextureView.display.rotation)
//                .build()
//                .also {
//                    it.setSurfaceProvider(backTextureView.surfaceProvider)
//                }

            val cameraSelector = CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
                .build()

//            val backCameraSelector = CameraSelector.Builder()
//                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
//                .build()
// 创建一个用于同时绑定前后摄像头的 CameraSelector 对象
            // 创建一个用于同时绑定前后摄像头的 CameraSelector 对象
//            MultiCameraSelector
//            UseCaseGroup.Builder().addUseCase(frontPreview,cameraSelector)
//                .addUseCase().build()
            cameraProvider.unbindAll()

            camera = cameraProvider.bindToLifecycle(
                this, cameraSelector, frontPreview
            )
//            camera2 = cameraProvider.bindToLifecycle(
//                lf, backCameraSelector, backPreview
//            )
        }, ContextCompat.getMainExecutor(this))

    }

    override fun onDestroy() {
        super.onDestroy()
        cameraExecutor.shutdown()
    }
}